package com.zhoufeng.common;

import java.util.ArrayList;
import java.util.List;

import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.weixin.demo.WeixinApiController;
import com.jfinal.weixin.demo.WeixinMsgController;
import com.zhoufeng.controller.IndexController;
import com.zhoufeng.model.DeviceConfigure;
import com.zhoufeng.model.DeviceModel;
import com.zhoufeng.model.Recievedata;
import com.zhoufeng.model.User;
import com.zhoufeng.model.UserDeviceModel;
import com.zhoufeng.schedule.MonitThread;
import com.zhoufeng.udp.service.Monitor;
import com.zhoufeng.udp.service.UDPServer;

public class DemoConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("a_little_config.txt");
		me.setMainRenderFactory(new BeetlRenderFactory());
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setUrlParaSeparator("-");
		me.setEncoding("UTF-8");
		me.setMaxPostSize(1999999999);
		

		List<Thread> list = new ArrayList<Thread>();
		System.out.println("UDP启动中......");
		String[] ports = getProperty("udpport").split(",");
		if (ports != null) {
			for (String port : ports) {
				try {
					Thread thread = new Thread(new UDPServer(new Integer(port)));
					thread.start();
					list.add(thread); 
					System.out.println("启动端口<" + port + ">成功!");
				} catch (NumberFormatException e) {
					System.out.println("启动端口<" + port + ">出现错误：" + e.getMessage());
					e.printStackTrace();
				}
			}
		}
		System.out.println("UDP启动完成......");
		
		System.out.println("设备在线监视器启动中......");
		Thread thread = new Thread(new MonitThread());
		thread.start();
		System.out.println("设备在线监视器启动成功......");
		//list.add(thread); 
		Thread m = new Thread(new Monitor(list));
		m.start();
		
		
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);

		me.add("/msg", WeixinMsgController.class);
		me.add("/api", WeixinApiController.class, "/api");

	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
		me.add(arp);
		arp.addMapping("thing_device_recievedata", Recievedata.class);
		arp.addMapping("thing_device", DeviceModel.class);
		arp.addMapping("thing_user", "userid", User.class);
		arp.addMapping("thing_device_info", DeviceConfigure.class);
		arp.addMapping("thing_user_device", "userid,deviceid", UserDeviceModel.class);

	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		// me.add(new SessionInteceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("base"));
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8080, "/", 5);
	}

}
